package com.example.edupublisher.controller;

import com.example.edupublisher.bean.CourseReviewStats;
import com.example.edupublisher.service.CourseStatsService;
import com.example.edupublisher.util.DateFormatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
@RestController
public class CourseStatsController {
    @Autowired
    private CourseStatsService courseStatsService;

    @RequestMapping("/courseReviewStats")
    public String getCourseReviewStats(
     @RequestParam(value = "date", defaultValue = "1") Integer date) {
        if (date == 1) {
            date = DateFormatUtil.now();
        }
        List<CourseReviewStats> courseReviewStats = courseStatsService.getCourseReviewStats(date);
        //[CourseReviewStats(courseName=JavaScript DOM, avgStars=5.0, reviewUserCt=1, goodReviewUserCt=1, goodReviewRate=1.0),
        // CourseReviewStats(courseName=RabbitMQ消息中间件, avgStars=5.0, reviewUserCt=1, goodReviewUserCt=1, goodReviewRate=1.0),
        // CourseReviewStats(courseName=CSS3特效实战, avgStars=8.0, reviewUserCt=2, goodReviewUserCt=1, goodReviewRate=0.5),
        // CourseReviewStats(courseName=JavaScript高级, avgStars=3.0, reviewUserCt=1, goodReviewUserCt=0, goodReviewRate=0.0),
        // CourseReviewStats(courseName=Git&GitHub, avgStars=4.0, reviewUserCt=1, goodReviewUserCt=0, goodReviewRate=0.0)]


        if (courseReviewStats == null || courseReviewStats.size() == 0) {
            return "";
        }
        StringBuilder rows = new StringBuilder("[");
        for (int i = 0; i < courseReviewStats.size(); i++) {
            CourseReviewStats courseReviewStat = courseReviewStats.get(i);

            String courseName = courseReviewStat.getCourseName();
            Double avgStars = courseReviewStat.getAvgStars();
            Integer reviewUserCt = courseReviewStat.getReviewUserCt();
            Integer goodReviewUserCt = courseReviewStat.getGoodReviewUserCt();
            Double goodReviewRate = courseReviewStat.getGoodReviewRate();

            rows.append("{\n" +
                    "\t\"course_name\": \"" + courseName + "\",\n" +
                    "\t\"avg_stars\": \"" + avgStars + "\",\n" +
                    "\t\"review_user_ct\": \"" + reviewUserCt + "\",\n" +
                    "\t\"good_review_user_ct\": \"" + goodReviewUserCt + "\",\n" +
                    "\t\"good_review_rate\": \"" + goodReviewRate + "\"\n" +
                    "}");
            if (i < courseReviewStats.size() - 1) {
                rows.append(",");
            } else {
                rows.append("]");
            }
        }
        String str = "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"columns\": [\n" +
                "      {\n" +
                "        \"name\": \"课程名称\",\n" +
                "        \"id\": \"course_name\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"平均评分\",\n" +
                "        \"id\": \"avg_stars\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"评价人数\",\n" +
                "        \"id\": \"review_user_ct\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"好评人数\",\n" +
                "        \"id\": \"good_review_user_ct\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"好评率\",\n" +
                "        \"id\": \"good_review_rate\"\n" +
                "      }\n" +
                "    ],\n" +
                "    \"rows\": " + rows + "\n" +
                "  }\n" +
                "}";
        String finalStr = str.replaceAll("\\\\", ",");

        return finalStr;
        //return courseReviewStats.toString();
    }


}
